home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 25 user and custom controls / usercontrolsdemo / pagingbar.ascx.vb < prev    next >
Encoding:
Text File  |  2002-03-19  |  4.7 KB  |  126 lines

  1. ' An improved version of the PagingBar user control
  2.  
  3.  
  4. ' UNCOMMENT NEXT LINE IF YOU WANT TO ENFORCE 
  5. ' PARTIAL CACHING VIA AN ATTRIBUTE
  6. '<PartialCaching(5, "none", "PageNumber", "")> _
  7. Public MustInherit Class PagingBar
  8.     Inherits System.Web.UI.UserControl
  9.     Implements IPostBackDataHandler
  10.  
  11.     Protected WithEvents btnLast As System.Web.UI.WebControls.Button
  12.     Protected WithEvents btnNext As System.Web.UI.WebControls.Button
  13.     Protected WithEvents btnPrevious As System.Web.UI.WebControls.Button
  14.     Protected WithEvents litPageNumber As System.Web.UI.WebControls.Literal
  15.     Protected WithEvents btnFirst As System.Web.UI.WebControls.Button
  16.  
  17. #Region " Web Form Designer Generated Code "
  18.  
  19.     'This call is required by the Web Form Designer.
  20.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  21.  
  22.     End Sub
  23.  
  24.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  25.         'CODEGEN: This method call is required by the Web Form Designer
  26.         'Do not modify it using the code editor.
  27.         InitializeComponent()
  28.     End Sub
  29.  
  30. #End Region
  31.  
  32.     ' this is a public event exposed to the outside
  33.     Public Event PageChanged(ByVal sender As Object, ByVal e As EventArgs)
  34.  
  35.     ' this is a public property exposed to the outside
  36.     ' it is just a wrapper on a property of the litPageNumber constituent control
  37.  
  38.     Public Property PageNumber() As Integer
  39.         Get
  40.             Return CInt(litPageNumber.Text.TrimStart)
  41.         End Get
  42.         Set(ByVal Value As Integer)
  43.             If Value >= 1 And Value <= PageCount AndAlso Value <> PageNumber Then
  44.                 ' this is where the PageNumber property is held
  45.                 litPageNumber.Text = Value.ToString
  46.                 ' update other controls' state
  47.                 UpdateButtonState()
  48.                 ' Let the parent form know that the page has changed
  49.                 RaiseEvent PageChanged(Me, EventArgs.Empty)
  50.             End If
  51.         End Set
  52.     End Property
  53.  
  54.     ' The PageCount property
  55.     ' unlike PageNumber, this value is held in the ViewState bag
  56.  
  57.     Private m_PageCount As Integer = -1
  58.  
  59.     Public Property PageCount() As Integer
  60.         Get
  61.             If m_PageCount < 0 Then
  62.                 Dim objValue As Object = Me.ViewState("PageCount")
  63.                 If Not objValue Is Nothing Then
  64.                     m_PageCount = CInt(objValue)
  65.                 Else
  66.                     ' this is the default value
  67.                     m_PageCount = 100
  68.                 End If
  69.             End If
  70.             Return m_PageCount
  71.         End Get
  72.         Set(ByVal Value As Integer)
  73.             If Value >= 1 Then
  74.                 m_PageCount = Value
  75.                 UpdateButtonState()
  76.                 ' Save in the page's viewstate.
  77.                 Me.ViewState("PageCount") = Value
  78.             End If
  79.         End Set
  80.     End Property
  81.  
  82.     ' Update buttons' state
  83.     Sub UpdateButtonState()
  84.         btnFirst.Enabled = (PageNumber > 1)
  85.         btnPrevious.Enabled = (PageNumber > 1)
  86.         btnNext.Enabled = (PageNumber < PageCount)
  87.         btnLast.Enabled = (PageNumber < PageCount)
  88.     End Sub
  89.  
  90.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  91.         'Put user code to initialize the page here
  92.     End Sub
  93.  
  94.     ' react to clicks on navigational buttons
  95.  
  96.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  97.         PageNumber = 1
  98.     End Sub
  99.  
  100.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  101.         PageNumber = PageCount
  102.     End Sub
  103.  
  104.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  105.         PageNumber -= 1
  106.     End Sub
  107.  
  108.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  109.         PageNumber += 1
  110.     End Sub
  111.  
  112.     ' this method is invoked when a postback occurs
  113.     Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
  114.         ' we don't need to do anything special here
  115.         Return False
  116.     End Function
  117.  
  118.     ' this method is invoked after all the controls in the parent form
  119.     ' have processed the LoadPostData method, and only if the 
  120.     ' control returned True in that method 
  121.     ' (hence it will never been invoked in this demo)
  122.     Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
  123.         '
  124.     End Sub
  125. End Class
  126.